﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace project
{
    public partial class EmployeeUpdate : Form
    {
        public EmployeeUpdate()
        {
            InitializeComponent();
            Init();
        }
        public void Init()
        {
            string sql = "SELECT * FROM [dbo].[AdminTable]";
            DataTable dt = DBHelper.GetTable(sql);
            this.dataGridView1.DataSource = dt;
            this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            

        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void EmployeeUpdate_Load(object sender, EventArgs e)
        {
            string sql = "SELECT * FROM [dbo].[AdminTable] a join Department d on a.DepartmentNo=d.DepartmentNo";
            DataTable dt = DBHelper.GetTable(sql);
            string userId = this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
            foreach (DataRow item in dt.Rows)
            {
                if (item["adminNo"].ToString() == userId)
                {
                    this.comboBox1.SelectedItem = item["DepartName"];
                    this.textBoxName.Text = item["AdminName"].ToString();
                    this.textBoxSalary.Text = item["Salary"].ToString();
                    this.textBoxPhone.Text = item["Phone"].ToString();
                    this.textBoxAddress.Text = item["Address"].ToString();
                }
            }
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count==0)
            {
                return;
            }
            string sql = "SELECT * FROM [dbo].[AdminTable] a join Department d on a.DepartmentNo=d.DepartmentNo";
            DataTable dt = DBHelper.GetTable(sql);
            string userId = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            foreach (DataRow item in dt.Rows)
            {
                if (item["adminNo"].ToString() == userId)
                {
                    this.comboBox1.SelectedItem = item["DepartName"];
                    this.textBoxName.Text = item["AdminName"].ToString();
                    this.textBoxSalary.Text = item["Salary"].ToString();
                    this.textBoxPhone.Text = item["Phone"].ToString();
                    this.textBoxAddress.Text = item["Address"].ToString();
                    this.textBoxPassword.Text = item["Password"].ToString();
                    string sex= item["Sex"].ToString();
                    if (sex.Contains("男")) { 
                        this.radioButton1.Checked = true;
                    }
                    else { this.radioButton2.Checked = true; }    
                    
                }
            }
        }

        private void buttonUpdate_Click(object sender, EventArgs e)
        {
            string name= this.textBoxName.Text;
            string password = this.textBoxPassword.Text;
            string address = this.textBoxAddress.Text;
            string salary = this.textBoxSalary.Text;
            string phone = this.textBoxPhone.Text;
            int userId = Convert.ToInt32(this.dataGridView1.CurrentRow.Cells[0].Value);
            string sex;
            if (this.radioButton1.Checked)
            {
                 sex = "男";
            }
            else { 
            sex = "女";
            }
            string department = this.comboBox1.SelectedItem.ToString();
            string sql = "UPDATE [dbo].[AdminTable]SET [DepartmentNo] = (select [DepartmentNo] from Department where DepartName='{0}'),[AdminName] = '{1}',[Sex] = '{2}',[Password] ='{3}',[Salary] ='{4}',[Address] ='{5}',[Phone] = '{6}' WHERE adminNo={7}";
            sql = string.Format(sql,department,name,sex,password,salary,address,phone, userId);
            int re= DBHelper.ExecuteNonQuery(sql);
            if (re>0)
            {
                MessageBox.Show("修改成功");
                Init();
            }
            else
            {
                MessageBox.Show("修改失败");
            }
            
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            string userId= this.dataGridView1.CurrentRow.Cells[0].Value.ToString();
            string sql = "delete from  AdminTable where adminNo="+userId;
            int re= DBHelper.ExecuteNonQuery(sql);
            if (re>0)
            {
                MessageBox.Show("修改成功");
                Init();
            }
        }
    }
}
